// pages/edit/edit.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:1,
    cate:{},
    index:0,
    place:{},
    index1:0,
    array: ['使用中', '正常', '破损','报废', '待维修','借出'],
    index3:0,
    info:{},
    id:'',
    showCate:{},
    uid:'',
    username:''
  },
  inputedit:function(e){
    let dataset = e.currentTarget.dataset;
    //data-开头的是自定义属性，可以通过dataset获取到，dataset是一个json对象，有obj和item属性，可以通过这两个实现双向数据绑定，通过更改这两个值，对不同name的变量赋值
    let value = e.detail.value;
    this.data[dataset.obj][dataset.item] = value;
  },
  submit:function(){
    var config = {
      id:this.data.id,
      cid:this.data.info.cid,
      name:this.data.info.name,
      num:this.data.info.num,
      snum:this.data.info.snum,
      place:this.data.info.place,
      stime:this.data.info.stime,
      sale:this.data.info.sale,
      type:this.data.info.type,
      price:this.data.info.price,
      owner:this.data.info.owner,
    }
    for(var index in config){
        if(!config[index]){
          wx.showToast({
            title: '请输入合法参数',
            icon:'none'
          })
          return false;
        }
    }
    let info = this.data.info;
    if(info.type == '借出'){
      if(!info.borrow){
        wx.showToast({
          title: '请输入借用人',
          icon:'none'
        })
        return false;
      }
      if(!info.btime){
        wx.showToast({
          title: '请输入借用时间',
          icon:'none'
        })
        return false;
      }
      config.borrow = this.data.info.borrow
      config.btime = this.data.info.btime
    }
    wx.request({
      url: app.globalData.url+'api/zc/update',
      method: 'post',
      data:config,
      success:function(res){
        wx.showToast({
          title: '修改成功',
          duration:1000,
          success:function(){
            setTimeout(function () {
              //要延时执行的代码
              wx.navigateBack({//返回
                delta: 1
              })
            }, 1000) //延迟时间
          }
        })
      }
    })
  },
  bindDateChange1: function(e) {
    let info = this.data.info;
    info.btime = e.detail.value;
    this.setData({
      info:info
    })
  },
  bindDateChange: function(e){
    let info = this.data.info;
    info.stime  = e.detail.value;
    this.setData({
      info: info
    })
  },
  bindPickerChange: function(e){
    this.setData({
      index: e.detail.value
    })
    let info = this.data.info;
    info.cid = this.data.cate[e.detail.value]['id'];
    this.setData({
      info: info
    })
  },
  bindPickerChange1: function(e){
    this.setData({
      index1: e.detail.value
    })
    let info = this.data.info;
    info.place = this.data.place[e.detail.value]['id'];
    this.setData({
      info: info
    })
  },
  bindPickerChange2: function(e) {
    this.setData({
      index3: e.detail.value
    })
    let info =this.data.info;
    info.type  = this.data.array[e.detail.value];
    this.setData({
      info: info
    })
    if(e.detail.value == 5){
      this.setData({
        show:2
      })
    }else{
      this.setData({
        show:1
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options){
    if(!wx.getStorageSync('uid')){
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
    this.setData({
      id:options.id,
      uid:wx.getStorageSync('uid'),
      username:wx.getStorageSync('username')
    })
    this.getinfo();
    this.getCate();
    this.getPlace();
  },
// 获取存放地点
getPlace:function(){
  var that=this;
  wx.request({
    url: app.globalData.url+'api/info/place',
    method: 'get', 
    success:function(res){
      for(var index in res.data.data){
        if(res.data.data[index]['id'] == that.data.info.place){
          var kk = index; 
        }
      }
      that.setData({
        place:res.data.data,
        index1:kk
      })
    }
  })
},
getCate:function(){
  var that=this;
  wx.request({
    url: app.globalData.url+'api/cate/list',
    method: 'get', 
    success:function(res){
      for(var index in res.data.data){
        if(res.data.data[index]['id'] == that.data.info.cid){
          var kk = index; 
        }
      }
      that.setData({
        cate:res.data.data,
        index:kk
      })
    }
  })
},
getinfo:function(){
  var that=this;
  wx.request({
    url: app.globalData.url+'api/fixed/info',
    method: 'get', 
    data:{
      id:that.data.id
    },
    success:function(res){
      if(res.data.data.img){
        res.data.data.img = app.globalData.url+res.data.data.img
      }else{
          res.data.data.img = '../../../img/tupian.png'
      }
      if(!res.data.data.btime){
        res.data.data.btime = '2020-01-01'
      }
      that.setData({
        info:res.data.data,
      })
      if(res.data.data.type == '借出'){
        that.setData({
          show:2
        })
      }
    }
  })
},
upload:function(){
  var that =this;
  wx.chooseImage({
    success: function (res) {
      var tempFilePaths = res.tempFilePaths
      wx.uploadFile({
        url: app.globalData.url+'api/info/upload', //仅为示例，非真实的接口地址
        filePath: tempFilePaths[0],
        name: 'img',
        formData: {
          'id':that.data.id
        },
        success: function(res){
          wx.showToast({
            title: '上传成功',
          })
          let info = that.data.info;
          info.img = tempFilePaths[0];
          that.setData({
            info:info
          })
          //do something
        }
      })
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})